package com.xbs.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xbs.controller.base.BaseServlet;
import com.xbs.entity.CommonOrderEntity;
import com.xbs.entity.DetailsAdminEntity;
import com.xbs.entity.PagesEntity;
import com.xbs.entity.selfOrderAdminEntity;
import com.xbs.service.selfdriving.selfDrivingServiceAdmin;

@WebServlet("/selfDrivingAdmin")
public class AjaxSelfDrivingAdmin extends BaseServlet {
	//存储查询到的分页集合
		private PagesEntity<selfOrderAdminEntity> pagese;
		//服务层对象
		private selfDrivingServiceAdmin sa;
		private CommonOrderEntity comm;
		public AjaxSelfDrivingAdmin() {
			pagese = new PagesEntity<selfOrderAdminEntity>();
			sa = new selfDrivingServiceAdmin();
			comm = new CommonOrderEntity();
		}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//查看详情
		String find="";
		if(req.getParameter("find")!=null&&req.getParameter("find").length()!=0){
			find = req.getParameter("find");
		}
		String str="";
		if(find.equals("查看详情")){
			String findid = req.getParameter("findid");
			String kind = sa.getStatus(findid);
			DetailsAdminEntity detail = new DetailsAdminEntity();
			if(kind.equals("上门取还")){
				detail = sa.selDetail(findid);
			}else if(kind.equals("到店取还")){
				detail = sa.selDetailStore(findid);
			}
			str="<div class='findDialog' style='border:1px solid black'>"
				+ "<h3>查看详情</h3>"
				+ "<span>订单编号："+detail.getId()+"</span>"
				+ "<table cellspacing='0' cellpadding='0'  border='1' style='margin-top:5px;margin-bottom:10px;'>"
				+ "<tr height='50px' width='800px'>"
				+ "<td rowspan='3' width='240px'>"
				+ "<img src="+detail.getUrl()+" width='240px'>"
				+ "</td>"
				+ "<td width='140px'>租车人:</td>"
				+ "<td width='140px'>"+detail.getName()+"</td>"
				+ "<td width='140px'>联系电话:</td>"
				+ "<td width='140px'>"+detail.getTel()+"</td>"
				+ "</tr>"
				+ "<tr height='50px' width='800px'>"
				+ "<td width='200px'>车型:</td>"
				+ "<td width='200px'>"+detail.getType()+"</td>"
				+ "<td width='200px'>车牌号:</td>"
				+ "<td width='200px'>"+detail.getCard()+"</td>"
				+ "</tr>"
				+ "<tr height='50px' width='800px'>"
				+ "<td width='200px'>订单状态:</td>"
				+ "<td width='200px'>"+detail.getStatus()+"</td>"
				+ "<td width='200px'>取还方式:</td>"
				+ "<td width='200px'>"+detail.getKind()+"</td>"
				+ "</tr>"
				+ "</table>"
				+ "<table cellspacing='0' cellpadding='0'  border='1' style='margin-top:5px;margin-bottom:10px;'>"
				+ "<tr height='50px' width='800px'>"
				+ "<td width='200px'>取车时间:</td>"
				+ "<td width='200px'>"+detail.getGettime()+"</td>"
				+ "<td width='200px'>还车时间:</td>"
				+ "<td width='200px'>"+detail.getReturntime()+"</td>"
				+ "</tr>"
				+ "<tr height='50px' width='800px'>"
				+ "<td width='200px'>取车地点:</td>"
				+ "<td width='200px'>"+detail.getGetaddress()+"</td>"
				+ "<td width='200px'>还车地点:</td>"
				+ "<td width='200px'>"+detail.getReturnaddress()+"</td>"
				+ "</tr>"
				+ "</table>"
				+ "<table cellspacing='0' cellpadding='0'  border='1' style='margin-top:5px;margin-bottom:10px;'>"
				+ "<tr height='50px' width='800px'>"
				+ "<td width='100px'>租金:</td>"
				+ "<td width='100px'>"+detail.getRent()+"</td>"
				+ "<td width='100px'>押金:</td>"
				+ "<td width='100px'>"+detail.getGuarantee()+"</td>"
				+ "<td width='100px'>基本保险费:</td>"
				+ "<td width='100px'>"+detail.getInsurance()+"</td>"
				+ "<td width='100px'>车辆破损费:</td>"
				+ "<td width='100px'>"+detail.getDamage()+"</td>"
				+ "</tr>"
				+ "<tr height='50px' width='800px'>"
				+ "<td width='100px'>服务费（异店）:</td>"
				+ "<td width='100px'>"+detail.getPei()+"</td>"
				+ "<td width='100px'>应付金额:</td>"
				+ "<td width='100px'>"+detail.getPlan()+"</td>"
				+ "<td width='100px'>实收金额:</td>"
				+ "<td width='100px'>"+detail.getFact()+"</td>"
				+ "<td width='100px'>实退金额:</td>"
				+ "<td width=100px'>"+detail.getTui()+"</td>"
				+ "</tr>"
				+ "</table>"
				+ "<table cellspacing='0' cellpadding='0'  border='1' style='margin-top:5px;margin-bottom:10px;'>"
				+ "<tr height='50px' width='800px'>"
				+ "<td width='200px'>订单提交时间:</td>"
				+ "<td width='200px'>"+detail.getSubtime()+"</td>"
				+ "<td width='200px'>订单付款时间:</td>"
				+ "<td width='200px'>"+detail.getFutime()+"</td>"
				+ "</tr>"
				+ "<tr height='50px' width='800px'>"
				+ "<td width='200px'>订单完成时间:</td>"
				+ "<td width='200px'>"+detail.getFintime()+"</td>"
				+ "<td width='200px'>订单取消时间:</td>"
				+ "<td width='200px'>"+detail.getCantime()+"</td>"
				+ "</tr>"
				+ "</table>"
				+ "<table cellspacing='0' cellpadding='0'  border='1' style='margin-top:5px;margin-bottom:10px;'>"
				+ "<tr height='50px' width='800px'>"
				+ "<td width='200px'>续租开始时间:</td>"
				+ "<td width='100px'>"+detail.getRestarttime()+"</td>"
				+ "<td width='200px'>结束日期时间:</td>"
				+ "<td width='100px'>"+detail.getReendtime()+"</td>"
				+ "<td width='150px'>续租金额:</td>"
				+ "<td width='50px'>"+detail.getReletmoney()+"</td>"
				+ "</tr>"
				+ "</table>"
				+ "<input type='button' value='关闭' class='btn pointer' style='margin-left:380px;' id='close' onclick='cl()'>"
				+ "</div>";
		}else{
			
			//获取用户进行的操作
			String caozuo="";
			if(req.getParameter("caozuo")!=null&&req.getParameter("caozuo").length()!=0){
				caozuo = req.getParameter("caozuo");
			}
			
			//判断是否取消订单(获取订单编号)
			String order=req.getParameter("order");
			//获取系统当前时间
			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = sdf.format(d);
			if(caozuo.equals("取消")){
				//判断订单是什么状态
				String orderstatus = req.getParameter("orderstatus");
				if(order!=null&&order.length()!=0){
					 if(orderstatus.equals("预定成功")){
						 //计算最后实收金额和实付金额
						Double money =0.0;
						sa.upMoney(order, money);
						//更新应退金额
						Double xmoney = Double.parseDouble(req.getParameter("xmoney"));
						sa.upReturnMoney(order, xmoney);
					 }
					sa.cancelOrder(order,"已取消");
					sa.upCancelTime(order, time);
				}
			}else if(caozuo.equals("预定成功")){
				Double xmoney = Double.parseDouble(req.getParameter("xmoney"));
				//更新实付金额和实收金额
				sa.upMoney(order, xmoney);
				//更改订单状态
				sa.cancelOrder(order, "预定成功");
				//更改订单支付时间
				sa.upPayTime(order, time);
			}else if(caozuo.equals("租赁中")){
				//获取车牌号
				String carnum = req.getParameter("che");
				//写入数据库
				sa.upCarId(order, carnum);
				//更改订单状态
				sa.cancelOrder(order, "租赁中");
				//将车的状态改为1
				sa.upCarNum(carnum,1);
			}else if(caozuo.equals("已完成")){
				Double money=0.0;
				if(req.getParameter("famoney")!=null&&req.getParameter("famoney").length()!=0){
					money = Double.parseDouble(req.getParameter("famoney"));
				}
				
				if(req.getParameter("cpmoney")!=null&&req.getParameter("cpmoney").length()!=0){
					//获取车辆破损金额
					Double cpmoney = Double.parseDouble(req.getParameter("cpmoney"));
					//更新订单金额表中的车辆破损费
					sa.upDamageMoney(order, cpmoney);
					//更新订单表中的客户赔偿金额（车辆破损费）
					sa.upCompensation(order, cpmoney);
				}
				if(req.getParameter("ytmoney")!=null&&req.getParameter("ytmoney").length()!=0){
					//获取应退金额
					Double ytmoney = Double.parseDouble(req.getParameter("ytmoney"));
					sa.upReturnMoney(order, ytmoney);
				}
				//更新最后实收金额和实付金额
				sa.upMoney(order, money);
				//更改订单完成时间
				sa.upFinishTime(order, time);
				//更改订单状态
				sa.cancelOrder(order, "已完成");
				//将车的状态改为1
				String carnum=sa.selCarNum(order);
				sa.upCarNum(carnum,0);
				//根据订单编号查询该订单
				comm = sa.selAddress(order);
				if(comm.getCo_get_kind().equals("到店取还")){
					if(!comm.getCo_get_store().equals(comm.getCo_return_store())){
						sa.insHitch(comm.getCo_car_id(), Integer.parseInt(comm.getCo_get_store()), Integer.parseInt(comm.getCo_return_store()),"闲置中");
					}
					
				}
				
			}
			
			//获取备注
			String remark = "";
			if(req.getParameter("remark")!=null&&req.getParameter("remark").length()!=0){
				remark = req.getParameter("remark");
				//更新备注
				sa.upRemark(order, remark);
			}
			
			
			String fromdate="";
			String todate="";
			// 获取系统当前时间
			Date now = new Date();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			if(req.getParameter("fromdate") == null||req.getParameter("fromdate").length()==0){
				fromdate=df.format(now);
			}else{
				fromdate=req.getParameter("fromdate");
			}
			
			if(req.getParameter("todate") == null||req.getParameter("todate").length()==0){
				todate=df.format(now);
			}else{
				todate=req.getParameter("todate");
			}
			
			String tel = req.getParameter("tel");
			
		
			// 获取当前页
			String page = req.getParameter("currpage");
			// 获取每页大小
			String size = req.getParameter("size");
			//获取用户需要哪一页
			String status = req.getParameter("status");

			Integer currpage = page == null ? 1 : Integer.parseInt(page);
			Integer pagesize = size == null ? 10 : Integer.parseInt(size);
			
			//总记录数
			int total=sa.selOrderAdminEntityTotal(fromdate, todate, tel);
			//总页数
			int pages = (total-1)/pagesize+1;
			if(status.equals("首页")){
				currpage=1;
			}else if(status.equals("上一页")&&(currpage<=0||currpage>=pages)){
				currpage=1;
			}else if(status.equals("下一页")&&currpage>pages){
				currpage=pages;
			}else if(status.equals("尾页")){
				currpage=pages;
			}
			
			// 默认显示当天的所有订单
			pagese = sa.selAllco(fromdate, todate, tel, currpage, pagesize);
			
			
			if(pagese.getData().size()==0||pagese.getData()==null){
				str="<p style='font-size:26px;margin:20px 450px;margin-top:50px;width:200px;padding-left:70px'>温馨提示</p>"
						+ "<p style='margin-left:480px'>没有符合条件的订单，请重新查询</p>";
			}else{
				str="<tr>"
						+ "<th>编号</th>"
						+ "<th>状态</th>"
						+ "<th>应付金额</th>"
						+ "<th>实付金额</th>"
						+ "<th>实收金额</th>"
						+ "<th>押金</th>"
						+ "<th>电话</th>"
						+ "<th>实际租车人</th>"
						+ "<th>服务备注</th>"
						+ "<th>操作</th>"
						+ "<th>查看详情</th>"
						+ "</tr>";
				
				for(selfOrderAdminEntity item:pagese.getData()){
					str+="<tr>"
							+ "<td>"+item.getId()+"</td>"
							+ "<td>"+item.getStatus()+"</td>"
							+ "<td>"+item.getPmoney()+"</td>"
							+ "<td>"+item.getMoney()+"</td>"
							+ "<td>"+item.getFmoney()+"</td>"
							+ "<td>"+item.getYmoney()+"</td>"
							+ "<td>"+item.getTel()+"</td>"
							+ "<td>"+item.getName()+"</td>"
							+ "<td>"+item.getRemark()+"</td>"
							+ "<td>"
							+ "<input class='update pointer' type='button' value='取消' onclick='getStatus(\""+item.getStatus()+"\");cancel(\""+item.getId()+"\");getpMoney(\""+item.getPmoney()+"\");getMoney(\""+item.getMoney()+"\");getfMoney(\""+item.getFmoney()+"\");getRemark(\""+item.getRemark()+"\");'/>"
							+ "<input class='update pointer' type='button' value='修改' onclick='getStatus(\""+item.getStatus()+"\");finish(\""+item.getId()+"\");getpMoney(\""+item.getPmoney()+"\");getfMoney(\""+item.getFmoney()+"\");getRemark(\""+item.getRemark()+"\");getYajin(\""+item.getYmoney()+"\");' style='margin-left:5px;background-color:#4BCF99'/>"
							+ "</td>"
							+ "<td>"
							+ "<input class='update pointer' type='button' value='查看详情' style='width: 60px;' onclick = 'detail(\""+item.getId()+"\");getOrder();'/>"
							+ "</td>"
							+ "</tr>";
				}
			}
			
		}
		
		resp.getWriter().print(str);
	}
}
